package com.github.postsanf.yinian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.fragment.GuideFragment1;
import com.github.postsanf.yinian.fragment.GuideFragment2;
import com.github.postsanf.yinian.fragment.GuideFragment3;
import com.github.postsanf.yinian.fragment.GuideFragment4;
import com.github.postsanf.yinian.utils.SpUtils;
import com.github.postsanf.yinian.utils.UIHelper;

import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends FragmentActivity {
    private ViewPager viewpager;
    private ImageView guide1_title, guide2_title, guide3_title;
    private ImageView guide1_content, guide2_content, guide3_content;
    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    private float currentPosition = 0;
    private GuideFragment1 fragment01;
    private ImageView indicator_iv1, indicator_iv2, indicator_iv3, indicator_iv4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_welcome);
        fragment01 = new GuideFragment1();
        fragmentList.add(fragment01);
        fragmentList.add(new GuideFragment2());
        fragmentList.add(new GuideFragment3());
        fragmentList.add(new GuideFragment4());
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        guide1_title = (ImageView) findViewById(R.id.guide1_title);
        guide2_title = (ImageView) findViewById(R.id.guide2_title);
        guide3_title = (ImageView) findViewById(R.id.guide3_title);
        guide1_content = (ImageView) findViewById(R.id.guide1_content);
        guide2_content = (ImageView) findViewById(R.id.guide2_content);
        guide3_content = (ImageView) findViewById(R.id.guide3_content);
        indicator_iv1 = (ImageView) findViewById(R.id.indicator_iv1);
        indicator_iv2 = (ImageView) findViewById(R.id.indicator_iv2);
        indicator_iv3 = (ImageView) findViewById(R.id.indicator_iv3);
        indicator_iv4 = (ImageView) findViewById(R.id.indicator_iv4);
        viewpager.setOffscreenPageLimit(3);
        viewpager.setAdapter(new GuideAdapter(getSupportFragmentManager()));
        viewpager.setPageTransformer(true, new HKTransformer());
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            private int lastValue = -1;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (positionOffset != 0) {
                    if (lastValue >= positionOffsetPixels) {
                        //右滑
                        switch (position) {
                            case 0:
                                indicator_iv1.setImageResource(R.drawable.iv_yellow_round);
                                indicator_iv2.setImageResource(R.drawable.iv_gray_round);
                                break;
                            case 1:
                                indicator_iv2.setImageResource(R.drawable.iv_yellow_round);
                                indicator_iv3.setImageResource(R.drawable.iv_gray_round);
                                break;
                            case 2:
                                indicator_iv3.setImageResource(R.drawable.iv_yellow_round);
                                indicator_iv4.setImageResource(R.drawable.iv_gray_round);
                                break;
                        }
                    } else if (lastValue < positionOffsetPixels) {
                        //左滑
                        switch (position) {
                            case 0:
                                indicator_iv1.setImageResource(R.drawable.iv_gray_round);
                                indicator_iv2.setImageResource(R.drawable.iv_yellow_round);
                                break;
                            case 1:
                                indicator_iv2.setImageResource(R.drawable.iv_gray_round);
                                indicator_iv3.setImageResource(R.drawable.iv_yellow_round);
                                break;
                            case 2:
                                indicator_iv3.setImageResource(R.drawable.iv_gray_round);
                                indicator_iv4.setImageResource(R.drawable.iv_yellow_round);
                                break;
                        }
                    }
                }
                lastValue = positionOffsetPixels;
                if (positionOffset > 0) {
                    switch (position) {
                        case 0:
                            int ww = UIHelper.dip2px(WelcomeActivity.this, 14) - (int) (UIHelper.dip2px(WelcomeActivity.this, 8) * positionOffset);
                            indicator_iv1.setLayoutParams(new LinearLayout.LayoutParams(ww, LinearLayout.LayoutParams.MATCH_PARENT));
                            int ww0 = UIHelper.dip2px(WelcomeActivity.this, 6) + (int) (UIHelper.dip2px(WelcomeActivity.this, 8) * positionOffset);
                            LinearLayout.LayoutParams lpm = new LinearLayout.LayoutParams(ww0, LinearLayout.LayoutParams.MATCH_PARENT);
                            lpm.setMargins(UIHelper.dip2px(WelcomeActivity.this, 4), 0, 0, 0);
                            indicator_iv2.setLayoutParams(lpm);
                            break;
                        case 1:
                            int ww1 = UIHelper.dip2px(WelcomeActivity.this, 14) - (int) (UIHelper.dip2px(WelcomeActivity.this, 8) * positionOffset);
                            LinearLayout.LayoutParams lpm1 = new LinearLayout.LayoutParams(ww1, LinearLayout.LayoutParams.MATCH_PARENT);
                            lpm1.setMargins(UIHelper.dip2px(WelcomeActivity.this, 4), 0, 0, 0);
                            indicator_iv2.setLayoutParams(lpm1);
                            int ww11 = UIHelper.dip2px(WelcomeActivity.this, 6) + (int) (UIHelper.dip2px(WelcomeActivity.this, 8) * positionOffset);
                            LinearLayout.LayoutParams lpm11 = new LinearLayout.LayoutParams(ww11, LinearLayout.LayoutParams.MATCH_PARENT);
                            lpm11.setMargins(UIHelper.dip2px(WelcomeActivity.this, 4), 0, 0, 0);
                            indicator_iv3.setLayoutParams(lpm11);
                            break;
                        case 2:
                            int ww2 = UIHelper.dip2px(WelcomeActivity.this, 14) - (int) (UIHelper.dip2px(WelcomeActivity.this, 8) * positionOffset);
                            LinearLayout.LayoutParams lpm2 = new LinearLayout.LayoutParams(ww2, LinearLayout.LayoutParams.MATCH_PARENT);
                            lpm2.setMargins(UIHelper.dip2px(WelcomeActivity.this, 4), 0, 0, 0);
                            indicator_iv3.setLayoutParams(lpm2);
                            int ww22 = UIHelper.dip2px(WelcomeActivity.this, 6) + (int) (UIHelper.dip2px(WelcomeActivity.this, 8) * positionOffset);
                            LinearLayout.LayoutParams lpm22 = new LinearLayout.LayoutParams(ww22, LinearLayout.LayoutParams.MATCH_PARENT);
                            lpm22.setMargins(UIHelper.dip2px(WelcomeActivity.this, 4), 0, 0, 0);
                            indicator_iv4.setLayoutParams(lpm22);
                            break;
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {
                final boolean splashGuide = SpUtils.getBoolean(WelcomeActivity.this, "welcomeGuidestart");
                if (!splashGuide) {
                    if (position == 3) {
                        sendBroadcast(new Intent("updateGuideShow"));
                        SpUtils.put(WelcomeActivity.this, "welcomeGuidestart", true);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    class GuideAdapter extends FragmentPagerAdapter {
        public GuideAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

    class HKTransformer implements ViewPager.PageTransformer {
        @Override
        public void transformPage(View view, float position) {
            float p1 = -1;
            if (fragment01.getView() == view) {
                currentPosition = position;
            }
            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
            } else if (position <= 0) { // [-1,0]
                // Use the default slide transition when moving to the left page
            } else if (position <= 1) { // (0,1]
                // Fade the page out.
                float p = Math.abs(position);
                float f = (1 - p);
                // Log.i("", "p= " + p);
                // p : 1~0
                // f : 0~1
                if (-1 < currentPosition && currentPosition <= 0) {
                    // A ~ B
                    guide1_title.setTranslationX(-1200 * f);
                    guide1_content.setTranslationX(-1200 * f);
                    guide1_title.setAlpha(1.0f - 1.0f * f);
                    guide1_content.setAlpha(1.0f - 1.0f * f);
                    guide2_title.setTranslationX(2000 * p);
                    guide2_content.setTranslationX(2000 * p);
                    guide3_title.setVisibility(View.GONE);
                    guide3_content.setVisibility(View.GONE);
                    p1 = p;
                    if (p1 != 1.0) {

                    }
                } else if (-2 < currentPosition && currentPosition <= -1) {
                    // B ~ C
                    guide3_title.setVisibility(View.VISIBLE);
                    guide3_content.setVisibility(View.VISIBLE);
                    guide2_title.setTranslationX(-1200 * f);
                    guide2_content.setTranslationX(-1200 * f);
                    guide2_title.setAlpha(1.0f - 1.0f * f);
                    guide2_content.setAlpha(1.0f - 1.0f * f);
                    guide3_title.setTranslationX(2000 * p);
                    guide3_content.setTranslationX(2000 * p);

                } else if (-3 < currentPosition && currentPosition <= -2) {
                    guide3_title.setTranslationX(-1200 * f);
                    guide3_content.setTranslationX(-1200 * f);
                    guide3_title.setAlpha(1.0f - 1.0f * f);
                    guide3_content.setAlpha(1.0f - 1.0f * f);

                }

            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
            }
        }
    }
}
